package patient

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

// GetDoctorList 根据关键词搜索医生列表（支持姓名、职称、擅长领域模糊搜索）
func GetDoctorList(c *gin.Context) {
	var form request.GetDoctorListReq
	if err := c.ShouldBindQuery(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	// 设置分页默认值和限制
	page, pageSize := normalizePage(form.Page, form.PageSize)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// 调用 gRPC 医生服务：注意后端只接受 Keyword 字段（不是 Department/Specialty）
	resp, err := config.PatientClient.GetDoctorList(ctx, &proto.GetDoctorListRequest{
		Page:     int32(page),
		PageSize: int32(pageSize),
		Keyword:  form.Keyword,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取医生列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": gin.H{
			"total":   resp.Total,
			"doctors": resp.Doctors,
		},
	})
}

// GetDoctorsByHospital 根据医院 ID 查询医生列表
func GetDoctorsByHospital(c *gin.Context) {
	var form request.GetDoctorsByHospitalReq
	if err := c.ShouldBindQuery(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	if form.HospitalId <= 0 {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "医院ID不能为空"})
		return
	}

	page, pageSize := normalizePage(form.Page, form.PageSize)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetDoctorsByHospital(ctx, &proto.GetDoctorsByHospitalRequest{
		HospitalId: form.HospitalId,
		Page:       int32(page),
		PageSize:   int32(pageSize),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "按医院查询医生失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": gin.H{
			"total":   resp.Total,
			"doctors": resp.Doctors,
		},
	})
}

// GetDoctorsByDepartment 根据科室名称查询医生
func GetDoctorsByDepartment(c *gin.Context) {
	var form request.GetDoctorsByDepartmentReq
	if err := c.ShouldBindQuery(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	if form.Department == "" {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "科室不能为空"})
		return
	}

	page, pageSize := normalizePage(form.Page, form.PageSize)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetDoctorsByDepartment(ctx, &proto.GetDoctorsByDepartmentRequest{
		Department: form.Department,
		Page:       int32(page),
		PageSize:   int32(pageSize),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "按科室查询医生失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": gin.H{
			"total":   resp.Total,
			"doctors": resp.Doctors,
		},
	})
}

// GetDoctorsByDisease 根据疾病关键词在“擅长领域”中匹配医生
func GetDoctorsByDisease(c *gin.Context) {
	var form request.GetDoctorsByDiseaseReq
	if err := c.ShouldBindQuery(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	if form.Disease == "" {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "疾病名称不能为空"})
		return
	}

	page, pageSize := normalizePage(form.Page, form.PageSize)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetDoctorsByDisease(ctx, &proto.GetDoctorsByDiseaseRequest{
		Disease:  form.Disease,
		Page:     int32(page),
		PageSize: int32(pageSize),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "按疾病查询医生失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": gin.H{
			"total":   resp.Total,
			"doctors": resp.Doctors,
		},
	})
}

// GetDoctorDetail 获取医生详情（通过 URL 路径参数 /:id）
func GetDoctorDetail(c *gin.Context) {
	doctorIdStr := c.Param("id")
	doctorId, err := strconv.ParseInt(doctorIdStr, 10, 64)
	if err != nil || doctorId <= 0 {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "医生ID格式错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetDoctorDetail(ctx, &proto.GetDoctorDetailRequest{
		DoctorId: doctorId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取医生详情失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp.Doctor,
	})
}
